package com.xbts.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.DateUtils;
import com.xbts.common.utils.SecurityUtils;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.domain.CnHelp;
import com.xbts.system.mapper.CnHelpMapper;
import com.xbts.system.service.ICnHelpService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 帮助中心Help业务层处理
 *
 * @author xbts
 * @date 2023-08-07
 */
@Service
public class CnHelpServiceImpl extends ServiceImpl<CnHelpMapper, CnHelp> implements ICnHelpService {
    /**
     * 查询帮助表
     *
     * @return help表
     * @param cnHelp
     */
    @Override
    public List<CnHelp> selectCnHelp(CnHelp cnHelp) {
        QueryWrapper<CnHelp> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(cnHelp.getHelpName()), "help_name", cnHelp.getHelpName());
        queryWrapper.last("order by help_parent_id,sort asc");
        List<CnHelp> table = list(queryWrapper);
        return getTree(table);
    }

    @Override
    public List<CnHelp> selectHelpList(CnHelp cnHelp) {
        LambdaQueryWrapper<CnHelp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CnHelp::getType, 1);
        queryWrapper.like(StringUtils.isNotEmpty(cnHelp.getHelpName()), CnHelp::getHelpName, cnHelp.getHelpName());
        queryWrapper.last("order by help_parent_id,sort asc");
        return getTree(list(queryWrapper));
    }
    /**
     * 新增内容
     *
     * @param cnHelp 新帮助内容
     * @return 结果
     */
    @Override
    public boolean insertCnHelp(CnHelp cnHelp) {
        if (null == cnHelp.getHelpParentId()){
            // 默认父级
            cnHelp.setHelpParentId("0");
        }
        cnHelp.setCreateBy(SecurityUtils.getUserId().toString());
        return save(cnHelp);
    }

    /**
     * 删除
     *
     * @param helpId 主键id
     * @return 结果
     */
    public boolean deleteContentById(Long helpId) {

        return removeById(helpId);
    }

    /**
     * 批量删除help文章
     *
     * @param ids 多个主键id
     * @return 结果
     */
    @Override
    public boolean batchDeleteContentById(List<String> ids) {
        LambdaQueryWrapper<CnHelp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CnHelp::getHelpParentId, ids);
        long count = count(queryWrapper);
        if (count > 0){
            throw new ServiceException("存在子级，请先删除子级！");
        }
        return removeBatchByIds(ids);
    }

    /**
     * 更新help文章
     *
     * @param cnHelp 帮助内容
     * @return 结果
     */
    public boolean updateHelp(CnHelp cnHelp) {
        cnHelp.setUpdateTime(DateUtils.getNowDate());
        cnHelp.setUpdateBy(SecurityUtils.getUserId().toString());
        return updateById(cnHelp);
    }

    @Override
    public List<CnHelp> selectCnHelpById(Long helpId, String helpName) {
        LambdaQueryWrapper<CnHelp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CnHelp::getType, 2);
        queryWrapper.eq(CnHelp::getHelpParentId, helpId);
        queryWrapper.like(StringUtils.isNotBlank(helpName), CnHelp::getHelpName, helpName);
        queryWrapper.orderByAsc(CnHelp::getSort);
        return list(queryWrapper);
    }

    private List<CnHelp> getTree(List<CnHelp> list) {
        Map<java.lang.String, CnHelp> map = list.stream().collect(Collectors.toMap(CnHelp::getHelpId, a -> a, (k1, k2) -> k1));
        List<CnHelp> rootList = list.stream().filter(c -> "0".equals(c.getHelpParentId())).collect(Collectors.toList());
        for (CnHelp cnHelp : list) {
            String helpParentId = cnHelp.getHelpParentId();
            if (!"0".equals(helpParentId)) {
                List<CnHelp> children = map.get(helpParentId).getChildren();
                if (children == null) {
                    map.get(helpParentId).setChildren(new ArrayList<>());
                }
                map.get(helpParentId).getChildren().add(cnHelp);
            }
        }
        return rootList;
    }
}